/**
 * api接口
 */
import pako from "pako"

import request from "~/utils/request"

const staticBaseUrl = "https://web.slpzb.com/rest/v1"

// api返回数据类型定义
type ApiRes<T> = {
  code: number
  data: T
  errMsg?: string
  msg?: string
}

export default {
  /**
   * 登录接口
   */
  async login(data: { username: string; password: string }) {
    return request<ApiRes<Object>>({
      baseUrl: staticBaseUrl,
      url: "account/jwtlogin/",
      method: "POST",
      options: {
        json: data
      }
    })
  },
  /**
   * 后端数据解释接口
   * @param data { extra: { [key: string]: any }; dataPackage: blob } extra为额外参数，dataPackage为gzip压缩后的html文件
   */
  async dataParser(data: { htmlContent: string;[key: string]: any }) {
    const contentHtmlCompress = pako.gzip(data.htmlContent)
    // 转blob
    const blob = new Blob([contentHtmlCompress], { type: "application/gzip" })
    // 转为FormData对象，表单形式提交
    const formData = new FormData()
    // 添加文件
    formData.append("file", blob, "html.gz")
    // 添加表单数据,data对象除了htmlContent属性，其他属性都作为表单数据
    for (const key in data) {
      if (key !== "htmlContent") {
        formData.append(key, data[key])
      }
    }
    return request<ApiRes<{ [key: string]: any }>>({
      url: "v2/dm/plr/parser/",
      method: "POST",
      options: {
        body: formData
      }
    })
  },
  /**
   * 图片ocr识别接口,使用七牛的图片hash
   */
  async imageOcr(data: { imageHash: string; platform: string }) {
    return request<ApiRes<{ [key: string]: any }>>({
      url: "v2/dm/plr/ocr/",
      method: "GET",
      options: {
        searchParams: data
      }
    })
  },
  /**
   * 获取班次列表
   */
  async getClass(params: { date: string; prefixId: number }) {
    return request<ApiRes<any[]>>({
      url: "v2/options/anchor_class/",
      method: "GET",
      options: {
        searchParams: params
      }
    })
  },

  /**
   * 获取场次列表
   */
  async getPlays(params: { class_id: number }) {
    return request<ApiRes<any[]>>({
      url: "v2/options/shipper_play/",
      method: "GET",
      options: {
        searchParams: params
      }
    })
  },
  /**
   * 获取场次主播数据列表
   */
  async getPlayLiveDataList(params: { prefix_id: number; day: string }) {
    return request<ApiRes<any[]>>({
      url: "v2/dm/plr/",
      method: "GET",
      options: {
        searchParams: params
      }
    })
  },
  /**
   * 添加场次直播数据
   */
  async addPlayLiveData(data: { [key: string]: any }) {
    return request<ApiRes<any>>({
      url: "v2/dm/plr/",
      method: "POST",
      options: {
        json: data
      }
    })
  },
  /**
   * 删除场次直播数据
   */
  async deletePlayLiveData(id: number) {
    return request<ApiRes<any>>({
      url: `v2/dm/plr/${id}/`,
      method: "DELETE"
    })
  },

  /** ==== 店铺评分部分相关接口 ==== **/
  // 检查是否已存在相同日期的数据
  async checkRepeat(params) {
    return request<ApiRes<any>>({
      url: "v2/dm/dss/checkRepeat/",
      method: "GET",
      options: {
        searchParams: params
      }
    })
  },
  // 获取数据校验的键数量
  async getKeysCountForDss(params) {
    return request<ApiRes<any>>({
      url: "v2/dm/dss/dataKeys/",
      method: "GET",
      options: {
        searchParams: params
      }
    })
  },
  // 上传数据到服务器
  async uploadDssData(data: { [key: string]: any }) {
    return request<ApiRes<any>>({
      url: "v2/dm/dss/uploadCapture/",
      method: "POST",
      options: {
        json: data
      }
    })
  },

  /** === 抖音BIC模块相关接口 === **/
  // 获取抖音店铺bic可用数量(V3版本，随时替换)
  async getDyShopBicCountV3(shopID: number) {
    return request<ApiRes<any>>({
      baseUrl: "https://web.slpzb.com/rest/v1/",
      url: "v3_desktop_bic_code/available_count/",
      method: "post",
      options: {
        json: { prefix_id: shopID }
      }
    })
  },
  // 获取抖音店铺bic可用数量
  async getDyShopBicCount(shopID: number) {
    return request<ApiRes<any>>({
      url: "v2/orderTools/bic_available_quantity/",
      method: "GET",
      options: {
        searchParams: { prefix_id: shopID }
      }
    })
  },
  // 上传抖音bic数据
  async uploadDyBicList(data: { prefix_id: number, biccode: string[] }) {
    return request<ApiRes<Object>>({
      baseUrl: staticBaseUrl,
      url: "apiBiccode/",
      method: "POST",
      options: {
        json: data
      }
    })
  },


  /** === 辅助类接口 === **/
  // 获取dy请求参数
  async getDyRequestParam(data: any) {
    return request<ApiRes<any>>({
      url: "v2/widget/extra_dy_get_param/",
      method: "POST",
      options: {
        json: data
      }
    })
  }
}
